import os
import gc
import time;
from utime import ticks_ms, ticks_diff
from machine import Pin

import machine
import neopixel
import time
# 设置灯带参数 ws2812
NUM_LEDS =  1  # 灯珠数量
PIN =  8  # 灯带数据引脚
BRIGHTNESS =  0.1  # 亮度系数=xff08;0.0到1.0=xff09;

# 初始化灯带对象
np =  neopixel.NeoPixel(machine.Pin(PIN), NUM_LEDS)

# 渐变函数
def gradient(color1, color2, steps):
    r1, g1, b1 =  color1
    r2, g2, b2 =  color2
    step_r =  (r2 - r1) / steps
    step_g =  (g2 - g1) / steps
    step_b =  (b2 - b1) / steps
    for i in range(steps):
        r =  int(r1 + (i * step_r))
        g =  int(g1 + (i * step_g))
        b =  int(b1 + (i * step_b))
        yield (r, g, b)

def gpio_test():
	# 渐变循环
	while True:
		# 渐变从红色到绿色
		print("渐变从红色到绿色")
		for color in gradient((255, 0, 0), (0, 255, 0), NUM_LEDS):
			np.fill((int(color[0]*BRIGHTNESS), int(color[1]*BRIGHTNESS), int(color[2]*BRIGHTNESS)))
			np.write()
			time.sleep(0.5)
		# 渐变从绿色到蓝色
		print("渐变从绿色到蓝色")
		for color in gradient((0, 255, 0), (0, 0, 255), NUM_LEDS):
			np.fill((int(color[0]*BRIGHTNESS), int(color[1]*BRIGHTNESS), int(color[2]*BRIGHTNESS)))
			np.write()
			time.sleep(0.5)
		# 渐变从蓝色到红色
		print("渐变从蓝色到红色")
		for color in gradient((0, 0, 255), (255, 0, 0), NUM_LEDS):
			np.fill((int(color[0]*BRIGHTNESS), int(color[1]*BRIGHTNESS), int(color[2]*BRIGHTNESS)))
			np.write()
			time.sleep(0.5)

if __name__ == '__main__':
    print('test esp32c3 gpio ws2812\n')
    gpio_test()
    
